import time

from appium.options.common import AppiumOptions
from appium import webdriver
from appium.webdriver.common.appiumby import AppiumBy

from ..common.common_page import SWITCH_ENV


class CashPos(object):

    def __init__(self):
        options = AppiumOptions()
        options.load_capabilities({
            "platformName": "Android",
            # App包名
            # POS包名
            # "appPackage": "plam.food.new.pos.app.beta",
            # KDS包名
            # "appPackage": "palm.catering.kds.app",
            # "app": "/Users/palmnet/Downloads/c59fd9d4e34140c0d426a6d04987f89e.apk",
            # POS活动名称
            # "appActivity": "plam.food.pos.app.ui.splash.SplashActivity",
            # KDS活动名称
            # "appActivity": "palm.catering.kds.app.ui.splash.SplashActivity",
            # 系统弹框默认给权限
            "autoGrantPermissions": True,
            "disableAndroidWatchers": True,
            # 使用Appium自带的键盘，而不是安卓的键盘
            # "unicodeKeyboard": True,
            # "noReset": False,
            "appium:ensureWebviewsHavePages": True,
            "appium:nativeWebScreenshot": True,
            "appium:newCommandTimeout": 3600,
            "appium:connectHardwareKeyboard": True

        })

        self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", options=options)

        # 获取设备的宽和高
        screen_size = self.driver.get_window_size()
        self.width = screen_size['width']
        self.height = screen_size['height']
        # 需要等一等让应用启动
        self.driver.implicitly_wait(2)


class RebootCashPos(object):
    """
    这里是进行重启App的初始化程序，会清除所有数据
    """

    def __init__(self, not_reset: bool = True):
        options = AppiumOptions()
        options.load_capabilities({
            "platformName": "Android",
            # POS包名
            "appPackage": f"{SWITCH_ENV}",
            # POS活动名称
            "appActivity": "plam.food.pos.app.ui.splash.SplashActivity",
            # 系统弹框默认给权限
            "autoGrantPermissions": True,
            "disableAndroidWatchers": False,
            # 使用Appium自带的键盘，而不是安卓的键盘
            # "unicodeKeyboard": True,
            "noReset": not_reset,
            "appium:ensureWebviewsHavePages": True,
            "appium:nativeWebScreenshot": True,
            "appium:newCommandTimeout": 3600,
            "appium:connectHardwareKeyboard": True

        })

        self.driver = webdriver.Remote("http://127.0.0.1:4723/wd/hub", options=options)
        # 授权Display Over The App
        self.driver.execute_script('mobile: shell', {
            'command': f'pm grant {SWITCH_ENV} android.permission.SYSTEM_ALERT_WINDOW',
            'args': []
        })

        # 获取设备的宽和高
        screen_size = self.driver.get_window_size()
        self.width = screen_size['width']
        self.height = screen_size['height']
        # 需要等一等让应用启动
        self.driver.implicitly_wait(2)